package leetcode.editor.cn;

import java.util.*;

/**
 * @id: 102
 * @title: 二叉树的层序遍历
 */
 
//给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。 
//
// 
//
// 示例： 
//二叉树：[3,9,20,null,null,15,7], 
//
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回其层序遍历结果： 
//
// 
//[
//  [3],
//  [9,20],
//  [15,7]
//]
// 
// Related Topics 树 广度优先搜索 二叉树 
// 👍 1132 👎 0

public class P102BinaryTreeLevelOrderTraversal {
    public static void main(String[] args) {
        Solution solution = new P102BinaryTreeLevelOrderTraversal().new Solution();
        // todo
        TreeNode node9 = new TreeNode(9,
                new TreeNode(30, null, null),
                null);
        TreeNode node20 = new TreeNode(20,
                new TreeNode(15, new TreeNode(50, null, null), null),
                new TreeNode(7, null, null));
        TreeNode root = new TreeNode(3, node9, node20);
        final List<List<Integer>> lists = solution.levelOrder(root);
        System.out.println(lists.toString());
    }
    
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    // BST，广度遍历
//    public List<List<Integer>> levelOrder(TreeNode root) {
//        if (root == null) {
//            return new ArrayList<>();
//        }
//        List<List<Integer>> ans = new ArrayList<>();
////        Set<TreeNode> vis = new HashSet<>();
//        List<TreeNode> currLeve = new ArrayList<>();
//        currLeve.add(root);
//        while (currLeve.size() > 0) {
//            List<Integer> leveAns = new ArrayList<>();
//            List<TreeNode> nextLeve = new ArrayList<>();
//            for (TreeNode node: currLeve) {
//                if (node == null
////                        || vis.contains(node)
//                ) {
//                    continue;
//                }
////                vis.add(node);
//                leveAns.add(node.val);
//                if (node.left != null)
//                    nextLeve.add(node.left);
//                if (node.right != null)
//                    nextLeve.add(node.right);
//            }
//            ans.add(leveAns);
//            currLeve = nextLeve;
//        }
//        return ans;
//    }

    private List<List<Integer>> ans = new ArrayList<>();
    // DST，深度遍历
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        level(root, 0);
        return ans;
    }

    private void level(TreeNode root, int level) {
        if (root == null) {
            return;
        }
        if (ans.size() < level + 1) {
            ans.add(level, new ArrayList<>());
        }
        List<Integer> integers = ans.get(level);
        integers.add(root.val);
        level(root.left, level+1);
        level(root.right, level+1);
    }

}
//leetcode submit region end(Prohibit modification and deletion)


}